$().ready(function() {
	validateRule();
});

$.validator.setDefaults({
	submitHandler : function() {
		save();
	}
});
function save() {
	$.ajax({
		cache : true,
		type : "POST",
		url : "/theme/save",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			name : {
				required : true,
				maxlength: 10
			}
		},
		messages : {
			name : {
				required : icon + "请输入专题名称",
				maxlength: $.validator.format("分类名称不能大于{0}个字符")
			}
		}
	})
}

/**
 * 主题图
 */
layui.use('upload', function () {
	var upload = layui.upload;
	//执行实例
	var uploadInst = upload.render({
		elem: '#topicImgUpload', //绑定元素
		url: '/image/upload',
		size: 1000,
		accept: 'file',
		done: function (r) {
			layer.msg(r.msg);
			$('#topicImgDiv').empty();
			$("#topicImgId").val(r.imgId);
			var _html = '<a target="_blank" href="' + r.fileName + '">\n' +
				'<img alt="image" class="feed-photo" src="' + r.fileName + '"></a>';
			$('#topicImgDiv').html(_html);
		},
		error: function (r) {
			layer.msg(r.msg);
		}
	});
});

layui.use('upload', function () {
	var upload = layui.upload;
	//执行实例
	var uploadInst = upload.render({
		elem: '#headImgUpload', //绑定元素
		url: '/image/upload',
		size: 1000,
		accept: 'file',
		done: function (r) {
			layer.msg(r.msg);
			$('#headImgDiv').empty();
			$("#headImgId").val(r.imgId);
			var _html = '<a target="_blank" href="' + r.fileName + '">\n' +
				'<img alt="image" class="feed-photo" src="' + r.fileName + '"></a>';
			$('#headImgDiv').html(_html);
		},
		error: function (r) {
			layer.msg(r.msg);
		}
	});
});